package com.example.neteaseapplication.util;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpUtil {
    /**
     * 从网络加载歌词
     *
     * @param id
     * @param callback
     */
    public static void requestLrcData(final int id, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
//        String MusicName = name.trim();//去掉空格
//        String NameId = ATagToJson.convertHtml("https://music.163.com/#/search/m/?s=" + MusicName, MusicName);
//        Request request = new Request.Builder().url("http://music.163.com/api/song/media?id=" + id).build();
        Request request = new Request.Builder().url("https://api.imjad.cn/cloudmusic/?type=lyric&id=" + id).build();
        okHttpClient.newCall(request).enqueue(callback);
    }


    public static String parseJOSNWithGSON(Response response) {
        String lrc = "";
        try {

            String ResponseData = response.body().string();
            JSONObject jsonObject = new JSONObject(ResponseData);
            lrc = jsonObject.getString("lyric");
        } catch (Exception e) {

        }
        return lrc;
    }

    /*public static String parseJOSNWithGSON(Response response , int c){
        try{
            String ResponsData = response.body().string();
            JSONObject jsonObject = new JSONObject(ResponsData);
            int count = Integer.parseInt(jsonObject.getString("count"));
            Log.i("TAG", "parseJOSNWithGSONCOUNT:"+count);
            if (count>=c){
                String result = jsonObject.getString("result");
                JSONArray jsonArray = new JSONArray(result);
                JSONObject jsonObject1 = jsonArray.getJSONObject(c-1);
                String url = jsonObject1.getString("lyric");
                Log.i("TAG", "parseJOSNWithGSON:1 "+url);
                return url;
            }else {
                Log.i("TAG", "parseJOSNWithGSON: "+c);
                return "";
            }
        }catch (Exception e){

        }
        return "";
    }*/
}
